/**
 * @title: MSManagerController
 * @projectName mscloud
 * @Description TODO
 * @Author
 * @Date 2022/3/2 9:38
 */
package com.atguigu.springcloud.cloudsentinelgatewaydevelop.controller;

import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.Result;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.bean.dto.vo.InstanceVO;
import com.atguigu.springcloud.cloudsentinelgatewaydevelop.service.NacosManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
public class MSManagerController {
    @Autowired
    NacosManagerService nacosManagerService;
    @Autowired
    NamingService namingService;
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("/getServiceCount")
    public Mono<Result> getServiceCount(){
        return nacosManagerService.getServiceCount().map(count->{
            Result result=new Result();
            result.setCode(200);
            result.setBody(count);
            result.setMsg("OK");
            return result;
        });
    }

    @RequestMapping("/getAllServiceInstance")
    public Mono<Result> getAllServiceInstance(int pageNo, int pageSize) throws NacosException {
        return nacosManagerService.getAllServiceInstance(pageNo,pageSize).map(serviceViewsList ->{
            Result result= new Result();
            result.setMsg("OK");
            result.setCode(200);
            result.setBody(serviceViewsList);
            return result;
        } );
    }

    @RequestMapping("/createService")
    public Mono<Result> createService(String serviceName,@RequestParam(required = false) String protectThreshold,@RequestParam(required = false) String metadata,@RequestParam(required = false) String selector) throws NacosException {

        if(StringUtils.isEmpty(protectThreshold)){
            protectThreshold="0.0";
        }
        if (StringUtils.isEmpty(metadata)){
            metadata="{}";
        }

        return nacosManagerService.createService(serviceName, protectThreshold, metadata, selector).map(
                ret->{
                    Result result =new Result();
                    result.setCode(ret.getStatusCodeValue());
                    result.setBody(ret.getBody());
                    if(result.getCode().equals(200)){
                        result.setMsg("OK");
                    }else {
                        result.setMsg("NO");
                    }
                    return result;
                }
        );

    }

    @RequestMapping("/deRegisterService")
    public Mono<Result> deRegisterService(String serviceName){
        return nacosManagerService.deRegisterService(serviceName).map(ret ->{
            Result result= new Result();
            result.setCode(ret.getStatusCodeValue());
            result.setBody(ret.getBody());
            if(result.getCode().equals(200)){
                result.setMsg("OK");
            }else {
                result.setMsg("NO");
            }
            return result;
        } );

    }

    @RequestMapping("/deRegisterInstance")
    public Mono<Result> deRegisterInstance(String serviceName,String ip,int port,Boolean ephemeral){
        return nacosManagerService.deRegisterInstance(serviceName, ip, port, ephemeral).map(ret ->{
            Result result= new Result();
            result.setCode(ret.getStatusCodeValue());
            result.setBody(ret.getBody());
            if(result.getCode().equals(200)){
                result.setMsg("OK");
            }else {
                result.setMsg("NO");
            }
            return result;
        } );

    }

    @RequestMapping("/updateService")
    public Mono<Result> updateService(String serviceName,String protectThreshold,String metadata,String selector){
        return nacosManagerService.updateService(serviceName,protectThreshold,metadata,selector).map(ret ->{
            Result result= new Result();
            result.setCode(ret.getStatusCodeValue());
            result.setBody(ret.getBody());
            if(result.getCode().equals(200)){
                result.setMsg("OK");
            }
            return result;
        } );
    }

    @RequestMapping("/updateInstance")
    public Mono<Result> updateInstance(@RequestBody InstanceVO instance){
        return nacosManagerService.updateInstance(instance).map(ret->{
            Result result= new Result();
            result.setCode(ret.getStatusCodeValue());
            result.setBody(ret.getBody());
            if(result.getCode().equals(200)){
                result.setMsg("OK");
            }else{
                result.setMsg("NO");
            }
            return result;
        });
    }

    @RequestMapping("/getAllConfig")
    public Mono<Result> getAllConfig(int pageNo,int pageSize){
        return nacosManagerService.getAllConfig(pageNo,pageSize).map(
              configs->{
                  Result result=new Result();
                  result.setBody(configs.getBody());
                  result.setCode(configs.getStatusCodeValue());
                  if(configs.getStatusCodeValue()==200){
                      result.setMsg("OK");
                      return result;
                  }else{
                      result.setMsg("NO");
                  }
                return result;
              }
        );
    }

    @RequestMapping("/addOrUpdateConfig")
    public Mono<Result> addOrUpdateConfig(String dataId,String content,String type){
       return nacosManagerService.addOrUpdateConfig(dataId,content,type).map(ret->{
           Result result=new Result();
           if(ret.equals(true)){
               result.setCode(200);
               result.setMsg("OK");
           }else{
               result.setCode(500);
               result.setMsg("NO");
           }
           return result;
       });
    }

    @RequestMapping("/deleteConfig")
    public Mono<Result> deleteConfig(String dataId){
        return  nacosManagerService.deleteConfig(dataId).map(ret->{
            Result result=new Result();
            if(ret.equals(true)){
                result.setCode(200);
                result.setMsg("OK");
            }else{
                result.setCode(500);
                result.setMsg("NO");
            }
            return result;
        });
    }
}
